﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace HomeBudget
{
    /// <summary>
    /// Interaction logic for RecurringExpenseWindow.xaml
    /// </summary>
    public partial class RecurringExpenseWindow : Window
    {
        private NewCategoryWindow categoryWindow = new NewCategoryWindow("...\\...\\ExpenseRecurringCategory.txt");
        public RecurringExpenseWindow()
        {
            InitializeComponent();
        }
        private void ComboBox_Loaded(object sender, RoutedEventArgs e)
        {
            categoryWindow.categories.Initialize();
            var comboBox = sender as ComboBox;
            comboBox.ItemsSource = categoryWindow.categories.GetCategoriesAsString();
            comboBox.SelectedIndex = 0;
        }
        private void ComboBox_Loaded_Frequency(object sender, RoutedEventArgs e)
        {
            var comboBox = sender as ComboBox;
            List<string> frequency = new List<string>();
            frequency.Add("Monthly");
            frequency.Add("On Year");
            frequency.Add("Weakly");
            frequency.Add("Two Weaks");
            frequency.Add("Ten Days");
            comboBox.ItemsSource = frequency;
            comboBox.SelectedIndex = 0;
        }

        private void onNewCategoryClick(object sender, RoutedEventArgs e)
        {
            categoryWindow = new NewCategoryWindow("...\\...\\ExpenseRecurringCategory.txt");
            categoryWindow.Show();
        }

        private void onAddClick(object sender, RoutedEventArgs e)
        {
            var mainWindow = Application.Current.Windows.OfType<MainWindow>().First(x => x is MainWindow);

            decimal amount;
            string item = this.NameOfItem.Text;
            try
            {
                if (item == null || InvalidItemName.NameOfWhiteSpaces(item)) // a valid name cant be of whitespaces
                    throw mainWindow.invalidItemName;
            }
            catch (Exception)
            {
                MessageBox.Show(mainWindow.invalidItemName.Message);
                return;
            }
            try
            {
                amount = decimal.Parse(this.Amount.Text);
                if (amount <= 0)
                    throw mainWindow.invalidMoneyNegativeNumber;
            }
            catch (Exception exc)
            {
                if (exc is InvalidMoney)
                {
                    MessageBox.Show(mainWindow.invalidMoneyNegativeNumber.Message);
                }
                else
                {
                    MessageBox.Show(mainWindow.invalidMoneyCast.Message);
                }
                return;
            }
            string selectedCategory = this.ComboBoxCategories.SelectedItem.ToString();
            string selectedFrequency = this.ComboBoxFrequency.SelectedItem.ToString();

            RecurringInterval interval;
            if (selectedFrequency == "Monthly") { interval = RecurringInterval.monthly; }
            else if (selectedFrequency == "On Year") { interval = RecurringInterval.year; }
            else if (selectedFrequency == "Weakly") { interval = RecurringInterval.weekly; }
            else if (selectedFrequency == "Two Weaks") { interval = RecurringInterval.biweekly; }
            else { interval = RecurringInterval.tenDays; }

            RecurringExpense expense = new RecurringExpense(amount, item, new Category(selectedCategory), interval);

            mainWindow.moneySystem.AddExpense(expense);

            Database.SaveToFile(mainWindow.moneySystem.GetExpensesAsMoney());

            Renderer.WriteInformation(mainWindow.listBoxGridEpense, mainWindow.moneySystem.GetExpensesAsMoney());

            mainWindow.ShowMoneyNow();

            this.ExpenseRecurringWindow.Close();
        }

        private void onMouseEnter(object sender, MouseEventArgs e)
        {
            this.ComboBoxCategories.ItemsSource = categoryWindow.categories.GetCategoriesAsString();
        }
    }
}
